package com.supreme.studyserver.service;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.supreme.commonboot.exception.BizException;
import com.supreme.studyserver.entity.MysqlTest;
import com.supreme.studyserver.entity.Thing;
import com.supreme.studyserver.mapper.MysqlTestMapper;
import com.supreme.studyserver.mapper.ThingMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MdsService {

    @Resource
    private MysqlTestMapper mysqlTestMapper;

    @Resource
    private ThingMapper thingMapper;

    public void normalMultiDataSource(String name, Integer type) {
        MysqlTest mysqlTest = new MysqlTest();
        mysqlTest.setFirstName(name);
        mysqlTest.setLastName(name);
        mysqlTestMapper.insert(mysqlTest);

        Thing thing = new Thing();
        thing.setTitle(name);
        thing.setType(type);
        thing.setPoint(9.99);
        thingMapper.insert(thing);

        if (type < 0) {
            throw new BizException("WRONG !!!");
        }
    }

    @DSTransactional
    public void transactionMultiDataSource(String name, Integer type) {
        MysqlTest mysqlTest = new MysqlTest();
        mysqlTest.setFirstName(name);
        mysqlTest.setLastName(name);
        mysqlTestMapper.insert(mysqlTest);

        Thing thing = new Thing();
        thing.setTitle(name);
        thing.setType(1);
        thing.setPoint(9.99);
        thingMapper.insert(thing);

        if (type < 0) {
            throw new BizException("WRONG !!!");
        }
    }
}
